<template>
  <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
               label-width="68px">
        <el-form-item label="采购单号" prop="nos">
          <el-input v-model="queryParams.nos" placeholder="请输入订单编号"
                    clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="订单日期" prop="ddDate">
          <el-date-picker clearable v-model="queryParams.ddDate"
                          type="date" value-format="yyyy-MM-dd" placeholder="请选择订单日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="供应商" prop="supplierName">
          <el-input v-model="queryParams.supplierName" placeholder="请输供应商"
                    clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>

<!--        <el-form-item label="付款状态" prop="payApplyState">-->
<!--          <el-select v-model="queryParams.payApplyState" placeholder="请选择付款状态">-->
<!--            <el-option-->
<!--              v-for="dict in dict.type.buy_pay_status"-->
<!--              :key="dict.value"-->
<!--              :label="dict.label"-->
<!--              :value="dict.value"-->
<!--            />-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table"
                :max-height="tableMaxHeight">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="采购编号" align="center" prop="nos" show-overflow-tooltip/>
        <el-table-column label="供应商名称" align="center" prop="supplierName" show-overflow-tooltip/>
        <el-table-column label="采购日期" align="center" prop="ddDate" width="180">
          <template slot-scope="scope">
           <!-- <span>{{ parseTime(scope.row.ddDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>-->
            <span>{{ parseTime(scope.row.ddDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="采购金额" align="center" prop="money"/>

        <el-table-column align="center" label="采购类型" prop="goodsType">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.goods_type" :value="scope.row.goodsType"/>
          </template>
        </el-table-column>

        <el-table-column align="center" label="到货状态" prop="arrivalStatus">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.daohuo_status" :value="scope.row.arrivalStatus"/>
          </template>
        </el-table-column>

        <el-table-column align="center" label="付款状态" prop="payApplyState">
          <template slot-scope="scope">
            <dict-tag  :options="dict.type.buy_pay_status" :value="scope.row.payApplyState"/>
          </template>
        </el-table-column>

        <el-table-column label="定金" align="center" prop="dingjin"/>
        <el-table-column label="已付款" align="center" prop="fukuan"/>
        <el-table-column label="剩余欠款" align="center" prop="shengyu"/>

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-money"
                       v-if="scope.row.goodsType == '0' && scope.row.payApplyState == '0'"
                       @click="payApplyRetainer(scope.row)" v-hasPermi="['develop:LvBuy:edit']">申请付定金
            </el-button>
            <el-button size="mini" type="text" icon="el-icon-money"
                       v-if="scope.row.goodsType == '0' && (scope.row.payApplyState == '1' || scope.row.payApplyState == '2')"
                       @click="payApplyArrears(scope.row)" v-hasPermi="['develop:LvBuy:edit']">申请付尾款
            </el-button>
            <el-button size="mini" type="text" icon="el-icon-document"
                       @click="caigou(scope.row)" v-hasPermi="['develop:LvBuy:edit']">采购明细
            </el-button>
            <el-popconfirm :title="`确认批量到货该采购下所有产品？`" @confirm="handleDaohuoCgd(scope.row)"
                           style="margin-left: 10px;">
              <el-button slot="reference" size="mini" type="text" icon="el-icon-sold-out"
                         v-hasPermi="['develop:LvBuy:edit']">批量到货
              </el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                  :limit.sync="queryParams.pageSize" @pagination="getList"/>
      <lvBuy-addFormList ref="formCg" @ok="getList"></lvBuy-addFormList>
      <caigou-detail-list ref="caigouDetailList" @ok="getList"></caigou-detail-list>
      <daohuo-form ref="daohuoform" @ok="getList"></daohuo-form>

      <pay-apply-retainer-form ref="payApplyRetainerForm" @ok="getList"></pay-apply-retainer-form>
      <pay-apply-arrears-form ref="payApplyArrearsForm" @ok="getList"></pay-apply-arrears-form>

    </div>
  </el-dialog>
</template>

<script>
import {deleteAction, getAction} from '@/api/manage'
import LvBuyAddFormList from '@/views/develop/lvBuy/module/LvBuyAddFormList'
import CaigouDetailList from "@/views/develop/lvBuy/module/CaigouDetailList.vue";
import DaohuoForm from "@/views/develop/lvBuy/module/DaohuoForm";
import PayApplyRetainerForm from "@/views/develop/payApply/module/PayApplyRetainerForm.vue";
import PayApplyArrearsForm from "@/views/develop/payApply/module/PayApplyArrearsForm.vue";

export default {
  name: "LvBuyListDeposit",
  dicts: ["goods_type", "pay_material_status",'daohuo_status','buy_pay_status'],
  components: {
    PayApplyArrearsForm,
    PayApplyRetainerForm,
    LvBuyAddFormList,
    CaigouDetailList,
    DaohuoForm,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      title: "付款申请单",
      open: false,
      // 选中数组
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购单表格数据
      dataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        mainId: null,
        nos: null,
        customerName: null,
        supplierId: null,
        supplierName: null,
        deptId: null,
        deptName: null,
        userId: null,
        payApplyState: null,
        state: null,
        goodsType: null,
        orderByColumn: 'a.nos',
        isAsc: "desc"
      },
      url: {
        list: '/lvBuy/lvBuy/list',
        removeCg: '/lvBuy/lvBuy/removeCg/',
        export: '/lvBuy/lvBuy/export',
        checkGoodsAllArrived: '/goodsSalebuyall/goodsSalebuyall/checkGoodsAllArrived',
        piliangdaohuo: '/lvBuy/lvBuy/piliangdaohuo/',

      },
      tableMaxHeight: window.innerHeight - 400,
    };
  },
  created() {
    this.getList();
    window.addEventListener('resize', this.getTableHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight)
  },
  methods: {
    handleDaohuoCgd(row) {
      const ids = row.id || this.ids;
      deleteAction(this.url.piliangdaohuo, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("批量到货成功");
      }).catch(() => {
      });
    },
    caigou(row) {
      this.$refs.caigouDetailList.showcaigou(row);
    },
    daohuo(row) {
      this.$refs.daohuoform.showdaohuo(row);
    },
    fapiao(row) {
      this.$refs.fapiaoform.showfapiao(row);
    },
    dakuan(row) {
      this.$refs.dakuanform.showdakuan(row);
    },
    yunfei(row) {
      this.$refs.yunfeiform.showyunfei(row);
    },
    zhifahuo(row) {
      this.$refs.zhifahuoform.showzhifahuo(row);
    },
    /** 查询采购单列表 */
    getList() {
      this.loading = true;
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    /* handleAdd() {
         this.$refs.form.handleAdd();
     },*/
    handleAddCg() {
      this.$refs.formCg.handleAddCg();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row);
    },
    handleDeleteCgd(row) {
      const ids = row.id || this.ids;
      deleteAction(this.url.removeCg, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      deleteAction(this.url.remove, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },


    payApplyRetainer(row) {
      this.$refs.payApplyRetainerForm.show(row);
    },

    payApplyArrears(row) {
      // 付尾款之前  先检查是否订单下所有 产品已经到货
      getAction(this.url.checkGoodsAllArrived, {mainId: row.id}).then(response => {
        if (response.code === 200) {
          if (response.data === 0) {
            this.$refs.payApplyArrearsForm.show(row);
          } else {
            this.$modal.msgError("订单下所有产品未全部到货，不能付尾款");
          }
        } else {
          this.$modal.msgError("系统网络问题，请联系管理员!");
        }
      }).catch(() => {
      });
    },

    show(isDeposit,payApplyState) {
      this.open = true;
      this.queryParams.isDeposit = isDeposit;
      this.queryParams.payApplyState = payApplyState;
      this.getList()
    }

  }
};
</script>
